package com.mew.slots.core.game.jg.room;

import cn.hutool.core.util.RandomUtil;
import com.mew.slots.core.common.game.BaseRoom;
import com.mew.slots.core.common.game.vo.BasePlayMsgs;
import com.mew.slots.core.game.jg.bo.JgInRoom;
import com.mew.slots.core.game.jg.bo.JgPlayMsgs;
import com.mew.slots.core.game.jg.roller.Controlline;

import java.util.ArrayList;
import java.util.Arrays;

public class JgRoom extends BaseRoom {

    final int _SCATTER = 13;

    final int _QW = 8;
    final int _EY = 9;
    final int _BZ = 10;
    final int _JG = 11;

    final int _QW_RATE = 5;
    final int _EY_RATE = 3;
    final int _BZ_RATE = 10;
    final int _JG_RATE = 20;
    int freeRate;
    JgInRoom inRoom;
    public JgRoom(int freeRate, JgInRoom inRoom){
        this.freeRate = freeRate;
        this.inRoom = inRoom;
        res = new JgPlayMsgs();
        row = 3;
        col = 5;
        _WILD = 12;
        spes = new ArrayList<Integer>(){{
            add(_SCATTER);
        }};
        rate = new int[][]{
                {0, 0,  5, 15, 50},      //9              0
                {0, 0,  5, 15, 50},      //10             1
                {0, 0,  5, 15, 50},      //J              2
                {0, 0,  5, 15, 50},      //Q              3
                {0, 0,  5, 20, 80},      //K              4
                {0, 0,  5, 15, 50},      //A              5

                {0, 0, 10, 25, 100},      //蘑菇           6
                {0, 0, 10, 25, 100},      //红花           7

                {0, 0, 10, 30, 150},      //青蛙           8
                {0, 0, 10, 30, 150},      //鳄鱼           9
                {0, 0, 10, 30, 150},      //豹子           10

                {0, 5, 15, 50, 300},      //金刚 蛋        11

                {0, 0,  0,   0,  0},      //WILD          12
                {0, 0,  0,   0,  0},      //SCATTER       13
        };
        alllines=new int[][]{
                {5,6,7,8,9},
                {0,1,2,3,4},
                {10,11,12,13,14},
                {0,6,12,8,4},
                {10,6,2,8,14},
                {5,1,7,13,9},
                {5,11,7,3,9},
                {0,1,7,13,14},
                {10,11,7,3,4},
                {0,6,2,8,4},
                {10,6,12,8,14},
                {5,1,2,3,9},
                {5,11,12,13,9},
                {0,6,7,8,4},
                {10,6,7,8,14},
                {5,6,2,8,9},
                {5,6,12,8,9},
                {5,1,7,3,9},
                {5,11,7,13,9},
                {0,1,2,8,14},
                {10,11,12,8,4},
                {0,6,12,13,14},
                {10,6,2,3,4},
                {5,6,7,3,14},
                {5,6,7,13,4},
                {0,6,7,8,14},
                {10,6,7,8,4},
                {10,11,7,3,9},
                {0,1,7,13,9},
                {10,6,12,13,14},
                {0,6,2,3,4},
                {0,6,12,8,9},
                {10,6,2,8,9},
                {5,1,7,3,4},
                {5,11,7,13,14},
                {0,6,7,8,9},
                {10,6,7,8,9},
                {5,1,2,3,4},
                {5,11,12,13,14},
                {5,6,7,8,4},
                {5,6,7,8,14},
                {0,1,7,3,4},
                {10,11,7,13,14},
                {0,1,2,8,4},
                {10,11,12,8,14},
                {5,6,2,3,4},
                {5,6,12,13,14},
                {0,1,7,8,4},
                {10,11,7,8,14},
                {10,6,2,8,4}
        };
    }

    public void checkaward(){
        int[] rl;
        int type;
        int num;

        for (int j = 0;j< iconLines.length;j++) {
            BasePlayMsgs.LineResult bgo = new BasePlayMsgs.LineResult();
            bgo.lineNum = j;
            rl = iconLines[j].clone();
            type = rl[0];
            num = 0;
            //不为特殊图标
            if (spes.contains(type)){
                continue;
            }

            //确定类型
            for(int i=0;i<rl.length;i++){
                if(rl[i]!=_WILD){
                    type=rl[i];
                    break;
                }
            }
            int wildRate = free ? freeRate : 1;

            for (int i = 0; i < rl.length; i++) {
                if (type==rl[i]) {
                    num++;
                }else if(rl[i] == _WILD){
                    if(free) {
                        wildRate *= 2;
                    }
                    num++;
                }else {
                    break;
                }
            }
            Integer rate0 = rate[type][num - 1];
            if (rate0>0){
                bgo.mult = rate0*wildRate;
                bgo.type = type;
                bgo.num = num;
                bgo.uvIdx = Arrays.stream(lines[j]).limit(num).toArray();
                res.lines.add(bgo);
            }

        }

        for(BasePlayMsgs.LineResult x:res.lines)
        {
            res.multiple += x.mult;
        }
    }

    @Override
    public void freeBouns(){
        JgPlayMsgs pmres= (JgPlayMsgs) res;
        int scatterNum = fsdb(_SCATTER);
        if(scatterNum>2){
            pmres.setFree((byte)1);
            pmres.setFreeNumber(10);
            pmres.setFreeRate(RandomUtil.randomInt(2,5));
        }
        if(!free) {
            disposeBonus(pmres);
        }
    }

    private void disposeBonus(JgPlayMsgs pmres){
        for (int e : endSlots) {
            if(e == _QW){
                inRoom.setQw(inRoom.getQw()+1);
            }else if(e == _EY){
                inRoom.setEy(inRoom.getEy()+1);
            }else if(e == _BZ){
                inRoom.setBz(inRoom.getBz()+1);
            }else if(e == _JG){
                inRoom.setJg(inRoom.getJg()+1);
            }
        }
        if(inRoom.getQw() >= 100){
            inRoom.setQw(0);
            pmres.setQwWin(res.getBet()*res.getLine()*_QW_RATE);
        }
        if(inRoom.getEy() >= 100){
            inRoom.setEy(0);
            pmres.setEyWin(res.getBet()*res.getLine()*_EY_RATE);
        }
        if(inRoom.getBz() >= 100){
            inRoom.setBz(0);
            pmres.setBzWin(res.getBet()*res.getLine()*_BZ_RATE);
        }
        if(inRoom.getJg() >= 100){
            inRoom.setJg(0);
            pmres.setJgWin(res.getBet()*res.getLine()*_JG_RATE);
        }
        pmres.setQw(inRoom.getQw());
        pmres.setEy(inRoom.getEy());
        pmres.setBz(inRoom.getBz());
        pmres.setJg(inRoom.getJg());
        pmres.setBounswin(pmres.getQwWin()+pmres.getEyWin()+pmres.getBzWin()+pmres.getJgWin());
    }
    @Override
    public int[] control(){
        Controlline controlline=new Controlline();
        switch (controlType) {
            case 1:
                return bigwin();
            case 2:
                return controlline.free();
            default:
                return controlline.empty();
        }
    }
}
